package main.Web;

import main.JdbcUtils;
import main.Service.ChatMessageService;
import main.ServiceImpli.ChatMessageServiceImpl;
import main.pojo.ChatMessage;
import main.pojo.User;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;
/**
 * 作者: ALIM-MASTIK
 * 创建实现: 2023/7/20 0:31
 * 项目名称：Test
 * 软件: IntelliJ IDEA
 */
@WebServlet("/Upload")
public class Upload extends HttpServlet {
    private ChatMessageService chatMessageService;

    // 实例化 ChatMessageService
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("来了");
//        ServletInputStream inputStream = req.getInputStream();
//        byte[]buffer=new byte[1024000];
//        int read= inputStream.read(buffer);
//        System.out.println(new String(buffer,0,read));
//        System.out.println(((User)req.getSession().getAttribute("user")).getUsername());
        System.out.println(121212);
        if (ServletFileUpload.isMultipartContent(req)){
            FileItemFactory fileItemFactory=new DiskFileItemFactory();
            ServletFileUpload servletFileUpload=new ServletFileUpload(fileItemFactory);
            try {
                List<FileItem> list=servletFileUpload.parseRequest(req);
                System.out.println(list.size());
                for (FileItem fileItem : list) {
                    if (fileItem.isFormField()){
                        System.out.println("表单项value"+fileItem.getFieldName());
                    }else {
                        Connection connection = JdbcUtils.getConnection();// 将图片数据插入到数据库的BLOB字段中
                        JdbcUtils.insertImageToDB(connection, fileItem);
                        chatMessageService = new ChatMessageServiceImpl(connection);
                        ChatMessage newChatMessage = new ChatMessage();
                        newChatMessage.setSenderID(((User)(req.getSession().getAttribute("user"))).getUserID());
                        newChatMessage.setReceiverID(((User)(req.getSession().getAttribute("Friend"))).getUserID());
                        newChatMessage.setChatRoomID(8);
                        newChatMessage.setMessageType("image");
                        newChatMessage.setMessageContent(null);
                        newChatMessage.setSendTime(new java.util.Date());
                        newChatMessage.setPhoto(fileItem.getInputStream().readAllBytes());
                        boolean createSuccess = chatMessageService.createChatMessage(newChatMessage);
                        System.out.println(createSuccess);
                        System.out.println("上传成功");
                    }
                }
            } catch (Exception e) {
                System.out.println(8989);
            }
        }
    }
}
